/*
 * 类名称:AddressAreaController.java
 * 包名称:com.lht.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-09-30 17:02:56        ZhengZhanWu     初版做成
 */
package com.lht.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lht.admin.annotation.SysLog;
import com.lht.admin.constants.DataValidConstant;
import com.lht.admin.entity.AddressAreaEntity;
import com.lht.admin.entity.AddressCityEntity;
import com.lht.admin.service.AddressAreaService;
import com.lht.admin.service.AddressCityService;
import com.lht.admin.utils.PageUtils;
import com.lht.admin.utils.R;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区表Controller
 *
 * @author ZhengZhanWu
 * @date 2019-09-30 17:02:56
 */
@RestController
@RequestMapping("addressarea")
public class AddressAreaController extends AbstractController {
    @Autowired
    private AddressAreaService addressAreaService;
    @Autowired
    private AddressCityService addressCityService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("addressarea:list")
    public R queryAll(@RequestParam Map<String, Object> params) {
        params.put("isDelete", DataValidConstant.IS_DELETE_FALSE_STR);
        List<AddressAreaEntity> list = addressAreaService.queryAll(params);

        return R.ok().put("list", list);
    }

    /**
     * 分页查询区表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/list")
    @RequiresPermissions("addressarea:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("isDelete", DataValidConstant.IS_DELETE_FALSE_STR);
        Page page = addressAreaService.queryPage(params);
        PageUtils pageUtil = new PageUtils(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("addressarea:info")
    public R info(@PathVariable("id") Long id) {

        AddressAreaEntity addressArea = addressAreaService.getById(id);
        Map<String, Object> params = new HashMap<>();
        params.put("code", addressArea.getCityCode());
        List<AddressCityEntity> addressCityEntities = addressCityService.queryAll(params);
        if (CollectionUtils.isNotEmpty(addressCityEntities)) {
            AddressCityEntity addressCityEntity = addressCityEntities.get(0);
            addressArea.setProvinceCode(addressCityEntity.getProvinceCode());
        }
        return R.ok().put("addressArea", addressArea);
    }

    /**
     * 新增区表
     *
     * @param addressArea addressArea
     * @return R
     */
    @SysLog("新增区表")
    @PostMapping("/save")
    @RequiresPermissions("addressarea:save")
    public R save(@RequestBody AddressAreaEntity addressArea) {

        addressAreaService.add(addressArea);

        return R.ok();
    }

    /**
     * 修改区表
     *
     * @param addressArea addressArea
     * @return R
     */
    @SysLog("修改区表")
    @PostMapping("/update")
    @RequiresPermissions("addressarea:update")
    public R update(@RequestBody AddressAreaEntity addressArea) {

        addressAreaService.update(addressArea);

        return R.ok();
    }

    /**
     * 根据主键删除区表
     *
     * @param ids ids
     * @return R
     */
    @SysLog("删除区表")
    @PostMapping("/delete")
    @RequiresPermissions("addressarea:delete")
    public R delete(@RequestBody Long[] ids) {
        addressAreaService.deleteBatch(ids);

        return R.ok();
    }
}
